

import javax.swing.*;

import java.math.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.MessageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

class RealtimeCalcPanel extends JPanel implements ActionListener
{
	private JButton confirmButton, 
					nextButton,
					exitButton,
					printButton;



	private JLabel labelPairNumber,
					labelAuthor,
					labelCount;
					
					

	
	private JTextField textFieldPairNumber,
						
						textFieldSample1Name,
						textFieldSample2Name,
						TextFieldTestGeneName,
						TextFieldControlGeneName,
						TextFieldTestGeneCtValue1,
						TextFieldControlgeneCtValue1,
						TextFieldTestGeneCtValue2,
						TextFieldControlgeneCtValue2;

	private JTextArea reviewTextArea, combinedTextArea,
						outPutTextArea;
	
	double volume = 0.0;
	int i = 0;
	Chemical [] c = new Chemical [100];
	
	int number = 0;
	
	
	public RealtimeCalcPanel ()
	{
		setLayout (new GridBagLayout());
			
		
		// the first line
		JPanel pairNumberPanel = new JPanel ();
		pairNumberPanel.setLayout(new FlowLayout (FlowLayout.LEFT));
			
		labelPairNumber = new JLabel ("How many pair of realtime PCR results to be calculated?");
		textFieldPairNumber = new JTextField (10);
		pairNumberPanel.add(labelPairNumber);
		pairNumberPanel.add(textFieldPairNumber);
		add (pairNumberPanel, getConstraints (0, 0, 3, 1, GridBagConstraints.WEST));
		
			
		
		
		confirmButton = new JButton ("Confirm");
		confirmButton.addActionListener (this);
		add (confirmButton, getConstraints (1, 1, 1, 1, GridBagConstraints.EAST));
		
			
		
		labelCount = new JLabel ("Pair No.1 ");
		add (labelCount, getConstraints (0, 2, 1, 1, GridBagConstraints.WEST));
		
		
		
		textFieldSample1Name = new JTextField ("sample 1", 10);
		textFieldSample1Name.setEditable(false);
		
		textFieldSample2Name = new JTextField ("sample 2", 10);
		textFieldSample2Name.setEditable(false);
		JPanel samplePanel = new JPanel ();
		samplePanel.setLayout(new FlowLayout (FlowLayout.LEFT));
		samplePanel.add(textFieldSample1Name);
		samplePanel.add(textFieldSample2Name);
				
		add (samplePanel, getConstraints (1, 3, 1, 1, GridBagConstraints.WEST));
		
				
		
		TextFieldTestGeneName = new JTextField ("Test gene", 10);
		TextFieldTestGeneName.setEditable(false);
		add (TextFieldTestGeneName, getConstraints (0, 4, 1, 1, GridBagConstraints.EAST));
		
		
		TextFieldTestGeneCtValue1 = new JTextField ("Ct value", 10);
		TextFieldTestGeneCtValue2 = new JTextField ("Ct value", 10);
		TextFieldTestGeneCtValue1.setEditable(false);
		TextFieldTestGeneCtValue2.setEditable(false);
		JPanel testGenePanel = new JPanel();
		testGenePanel.setLayout(new FlowLayout (FlowLayout.LEFT));
		testGenePanel.add(TextFieldTestGeneCtValue1);
		testGenePanel.add(TextFieldTestGeneCtValue2);
		
		add (testGenePanel, getConstraints (1, 4, 1, 1, GridBagConstraints.WEST));
		
		TextFieldControlGeneName = new JTextField ("Control gene", 10);
		TextFieldControlGeneName.setEditable(false);
		add (TextFieldControlGeneName, getConstraints (0, 5, 1, 1, GridBagConstraints.EAST));
		
			

		TextFieldControlgeneCtValue1 = new JTextField ("Ct value", 10);
		TextFieldControlgeneCtValue2 = new JTextField ("Ct value", 10);
		TextFieldControlgeneCtValue1.setEditable(false);
		TextFieldControlgeneCtValue2.setEditable(false);
		JPanel controlGenePanel = new JPanel ();
		controlGenePanel.setLayout(new FlowLayout (FlowLayout.LEFT));
		controlGenePanel.add(TextFieldControlgeneCtValue1);
		controlGenePanel.add(TextFieldControlgeneCtValue2);
		
		
		add (controlGenePanel, getConstraints (1, 5, 1, 1, GridBagConstraints.WEST));
				
		
		
		
				
		nextButton = new JButton ("Next");
		nextButton.addActionListener (this);
		nextButton.setEnabled(false);
		add (nextButton, getConstraints (1, 6, 1, 1, GridBagConstraints.EAST));
		
		reviewTextArea = new JTextArea ("Copy of your input", 7, 40);
		reviewTextArea.setLineWrap (true);
		reviewTextArea.setWrapStyleWord (true);
		reviewTextArea.setEditable(false);
		JScrollPane reviewScroll = new JScrollPane (reviewTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add (reviewScroll);
		add (reviewScroll, getConstraints (0, 9, 2, 5, GridBagConstraints.WEST));
		
		
		outPutTextArea = new JTextArea ("Calculation result ", 7, 40);
		outPutTextArea.setLineWrap (true);
		outPutTextArea.setWrapStyleWord (true);
		outPutTextArea.setEditable(false);
		JScrollPane outputScroll = new JScrollPane (outPutTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add (outputScroll);	
		add (outputScroll, getConstraints (0, 14, 2, 5, GridBagConstraints.WEST));
		
		printButton = new JButton("Print");
		exitButton = new JButton ("Exit");
		printButton.addActionListener (this);
		printButton.setEnabled (false);
		exitButton.addActionListener (this);
		JPanel ButtonPanel = new JPanel ();
		ButtonPanel.setLayout(new FlowLayout (FlowLayout.LEFT));
		ButtonPanel.add (printButton);
		ButtonPanel.add (exitButton);
		add (ButtonPanel, getConstraints (1, 20, 1, 1, GridBagConstraints.EAST));
		
		labelAuthor = new JLabel ("Developed by Wenlei Cao");
		add (labelAuthor, getConstraints (0, 20, 1, 1, GridBagConstraints.EAST));
		
		}
		
	
	

	private GridBagConstraints getConstraints (int gridx, int gridy, int gridwidth, int gridheight, int anchor) 
	{
		GridBagConstraints c = new GridBagConstraints ();
		c.insets = new Insets (5, 5,5, 5 );
		c.ipadx = 0;
		c.ipady = 0;
		c.gridx = gridx;
		c.gridy = gridy;
		c.gridwidth = gridwidth;
		c.gridheight = gridheight;
		c.anchor = anchor;
		return c;
	}
	
	public boolean isValidData1 ()
	{
		   return SwingValidator.isPresent(textFieldPairNumber, "Pairs of realtime PCR to be calculated" )
			&& SwingValidator.isInteger(textFieldPairNumber, "Pairs of realtime PCR to be calculated" )
		    ;
		    
	}
	
	public boolean isValidData2 ()
	{
		return SwingValidator.isPresent(textFieldSample1Name, "Sample 1 Name" )
				&& SwingValidator.isPresent(textFieldSample2Name, "Sample 2 Name" )
				&& SwingValidator.isPresent(TextFieldTestGeneName, "Test gene Name" )
				&& SwingValidator.isPresent(textFieldSample2Name, "Sample 2 Name" )
				&& SwingValidator.isPresent(TextFieldControlGeneName, "Control gene Name" )
				&& SwingValidator.isPresent(TextFieldTestGeneCtValue1, "Test gene Ct value of sample 1" )
				&& SwingValidator.isDouble(TextFieldTestGeneCtValue1, "Test gene Ct value of sample 1" )
				&& SwingValidator.isPresent(TextFieldTestGeneCtValue1, "Test gene Ct value of sample 2" )
				&& SwingValidator.isDouble(TextFieldTestGeneCtValue1, "Test gene Ct value of sample 2" )
				&& SwingValidator.isPresent(TextFieldControlgeneCtValue1, "Control gene Ct value of sample 1" )
				&& SwingValidator.isDouble(TextFieldControlgeneCtValue1, "Control gene Ct value of sample 1" )
				&& SwingValidator.isPresent(TextFieldControlgeneCtValue2, "Control gene Ct value of sample 2" )
				&& SwingValidator.isDouble(TextFieldControlgeneCtValue2, "Control gene Ct value of sample 2" )
			    ;
	}
	
	
	
	
		public void actionPerformed (ActionEvent e)
		{
			Object source = e.getSource ();
			
				
			
			
			if (source == confirmButton)
				{
				if (isValidData1())
					{
					number = Integer.parseInt (textFieldPairNumber.getText());
					
					nextButton.setEnabled(true);
					textFieldSample1Name.setEditable(true);
					textFieldSample2Name.setEditable(true);
					TextFieldTestGeneName.setEditable(true);
					TextFieldControlGeneName.setEditable(true);
					TextFieldTestGeneCtValue1.setEditable(true);
					TextFieldControlgeneCtValue1.setEditable(true);
					TextFieldTestGeneCtValue2.setEditable(true);
					TextFieldControlgeneCtValue2.setEditable(true);
					confirmButton.setEnabled(false);
					textFieldPairNumber.setEditable(false);
					reviewTextArea.setText("Total "+ textFieldPairNumber.getText()+ " Pairs of samples will be compared \n");
					textFieldSample1Name.addFocusListener(new AutoSelect());
					textFieldSample2Name.addFocusListener(new AutoSelect());
					TextFieldTestGeneName.addFocusListener(new AutoSelect());
					TextFieldControlGeneName.addFocusListener(new AutoSelect());
					TextFieldTestGeneCtValue1.addFocusListener(new AutoSelect());
					TextFieldControlgeneCtValue1.addFocusListener(new AutoSelect());
					TextFieldTestGeneCtValue2.addFocusListener(new AutoSelect());
					TextFieldControlgeneCtValue2.addFocusListener(new AutoSelect());
					}
				}
			else if (source == nextButton)
				{
				if (isValidData2())
				{
					
					String Sample1Name = textFieldSample1Name.getText();
					String Sample2Name = textFieldSample2Name.getText();
					String TestGeneName = TextFieldTestGeneName.getText();
					String ControlGeneName = TextFieldControlGeneName.getText();
					double TestGeneCtValue1 = Double.parseDouble (TextFieldTestGeneCtValue1.getText());
					double ControlgeneCtValue1 = Double.parseDouble (TextFieldControlgeneCtValue1.getText());
					double TestGeneCtValue2 = Double.parseDouble (TextFieldTestGeneCtValue2.getText());
					double ControlgeneCtValue2 = Double.parseDouble (TextFieldControlgeneCtValue2.getText());
					reviewTextArea.append("Pair No. " + (i+1) + "\t\t" +  Sample1Name + "\t" + Sample2Name + "\n"
										+ TestGeneName + "\t\t" +  TestGeneCtValue1 + "\t" + TestGeneCtValue2 + "\n" 
										+ ControlGeneName + "\t\t" +  ControlgeneCtValue1 + "\t" + ControlgeneCtValue2 + "\n\n"
										);
					
					
					if (i < number && i+1 != number)
					{
						labelCount.setText ("Pair No. " + (i+2));
						
						c [i] = new Chemical (Sample1Name, Sample2Name, TestGeneCtValue1, ControlgeneCtValue1, TestGeneCtValue2, ControlgeneCtValue2);
							
						i++;
						
												
					}
					else if (i < number && i+1 == number)
					{
						nextButton.setEnabled(false);
						c [i] = new Chemical (Sample1Name, Sample2Name, TestGeneCtValue1, ControlgeneCtValue1, TestGeneCtValue2, ControlgeneCtValue2);
						
						outPutTextArea.setText ("mRNA expression relative level" + "\n" );
						printButton.setEnabled(true);
						
						for (int j=0; j< number; j++)
						{
						outPutTextArea.append ("Pair No. " + (j+1)+":\n"); 
						outPutTextArea.append (c [j].getSample1Name () + " VS " + c [j].getSample2Name () + " is " + c[j].getdecimalfolderChange() + " folder \n");
						}
						
						
					}
				}	
				
			}
			else if (source == exitButton)
			{
				System.exit(0);
			}
			else if (source == printButton)
			{ 
				final MessageFormat header = new MessageFormat("Solution Maker written by Wenlei Cao");
				final MessageFormat footer = new MessageFormat("Page - {0}");
			

				SwingUtilities.invokeLater(new Runnable() {
		            public void run() {
		                try {
		                    combinedTextArea = new JTextArea (reviewTextArea.getText()+"\n\n" + outPutTextArea.getText());
		                    combinedTextArea.print(header, footer, true, null,
		                            null, true);
		                } catch (Exception ex) {
		                    ex.printStackTrace();
		                }
		            }
		        });
			}
			
		}
	}
